package tjs.study.mini.springbootstartsmini.autoconfigure.web;

import org.springframework.web.servlet.DispatcherServlet;
import tjs.study.mini.springbootstartsmini.boot.web.ServletContextInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @author nicky
 * @date 2021/9/9 下午12:33
 */
public class DispatcherServletRegistrationBean implements ServletContextInitializer {
    private DispatcherServlet servlet;
    private String servletName;

    public DispatcherServletRegistrationBean(String servletName, DispatcherServlet servlet) {
        this.servletName = servletName;
        this.servlet = servlet;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ServletRegistration.Dynamic dynamic = servletContext.addServlet(this.servletName, this.servlet);
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);// 优先级最高表示 最早被加载
    }
}
